// pages/detail/detail.js
const {
  globalData
} = getApp()
const {
  Detail,
  Car,
  Collection
} = require("../../utils/api");
const {
  Http
} = require("../../utils/http");
const {
  debounce
} = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    prodId: "", //当前id,
    detailList: [], //详情页数据
    swiImgs: [], //轮播图
    prodName: "", //商品描述
    brief: "", //商品简单描述,
    price: "", //商品价格
    content: "", //大图,
    show: false,
    pic: "", //当前图片
    skuList: [], //选择样式
    flag: false,
    defaultSelectedObj: "", //默认选择的对象
    allProperties: [], //所有选项的集合
    allPropRender: {}, //最终渲染的sku列表
    selectedProp: {}, // 选择的对象
    chooseArrs: [], //切换的数组
    propKeys: [], // 所有名称集合
    skuId: 0,
    icn: "prod-col.png",
    like: false
  },
  onChange(event) {
    console.log(event.detail);
  },
  //遮罩层方法
  onClickShow() {
    this.setData({
      show: true
    });
  },
  cloeHi() {
    console.log("11111")
    this.setData({
      show: false
    });
  },
  //打开遮罩层
  onOpen() {
    // console.log("11")
    this.setData({
      flag: true
    })
  },
  //关闭遮罩层
  onClose() {
    this.setData({
      flag: false
    })
  },
  //添加购物车
  onAddcar(e) {
    if (this.data.chooseArrs > 1) {
      let str = this.data.chooseArrs.join(' ') + " ";
      console.log(str)
      this.data.skuList.forEach((item, index) => {
        if (item.skuName == str) {
          console.log(item)
          this.setData({
            skuId: item.skuId
          })
        }
      })
      console.log(this.data.skuId)
      let param = {
        basketId: 0,
        count: 1,
        prodId: this.data.detailList.prodId,
        shopId: this.data.detailList.shopId,
        skuId: this.data.skuId
      }
      Car.addCar(param, (res) => {
        console.log(res)
        wx.showToast({
          title: "添加成功",
        })
      })

    }else{
      console.log()
      let param = {
        basketId: 0,
        count: 1,
        prodId: this.data.detailList.prodId,
        shopId: this.data.detailList.shopId,
        skuId: this.data.skuList[0].skuId
      }
      Car.addCar(param, (res) => {
        console.log(res)
        wx.showToast({
          title: "添加成功",
        })
      })
    }

  },

  noop() {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    console.log(this.data.allPropRender)
    console.log(globalData.prodId)
    Detail.getDetailList({
      prodId: globalData.prodId
    }, (res) => {
      console.log(res.data)
      //结构数据
      const {
        imgs,
        prodName,
        brief,
        price,
        content,
        pic,
        skuList
      } = res.data
      console.log(imgs.split(","))
      //赋值
      this.setData({
        detailList: res.data,
        swiImgs: imgs.split(","),
        prodName: prodName,
        brief: brief,
        price: price,
        content,
        pic: pic,
        skuList: skuList
      })

      this.groipSku()
    })
    console.log(this.data.allPropRender)
    //判断是否收藏
    // Http('/p/user/collection/addOrCancel',{prodId:this.data.prodId},"GET",(res)=>{
    //   console.log(res.data,"=============")
    //   this.setData({
    //     like:res.data
    //   })
    // })
  },

  //重组数据sku
  groipSku() {
    // console.log(this.data.skuList)
    const skuList = this.data.skuList //sku数据列表
    let defaultSelectedObj = this.data.defaultSelectedObj //默认选择规格
    const allProperties = this.data.allProperties //最终版数组
    const allPropRender = this.data.allPropRender //最终渲染的sku列表
    const chooseArrs = this.data.chooseArrs //选择数组
    const propKeys = this.data.propKeys
    const selectedProp = this.data.selectedProp
    for (var i = 0; i < skuList.length; i++) {
      // console.log(skuList[0].properties)
      //设置默认选择规格
      defaultSelectedObj = skuList[0].properties
      //第一次通过;号截取
      //["版本:换修无忧版", "颜色:深空灰色", "内存:64GB"]
      const jie1 = skuList[i].properties.split(";")
      //组合数据
      for (var j = 0; j < jie1.length; j++) {
        //第二次截取:号截取
        const jie2 = jie1[j].split(":")
        if (i == 0) {
          propKeys.push(jie2[0])
          chooseArrs.push(jie2[1])
          selectedProp[jie2[0]] = jie2[1]
        }
        // console.log(jie2)
        if (!allPropRender[jie2[0]]) { //判断obj对象中有没有key如果没有则创建这个数据
          allPropRender[jie2[0]] = [];
          allPropRender[jie2[0]].push(jie2[1])
        } else {
          if (allPropRender[jie2[0]] && !allPropRender[jie2[0]].includes(jie2[1])) {
            allPropRender[jie2[0]].push(jie2[1])
          }
        }
        this.setData({
          chooseArrs
        })
      }
      this.setData({
        allPropRender: allPropRender
      })
      console.log(this.data.allPropRender)

    }
  },


  //切换高亮
  chooseItem(e) {
    console.log(e.currentTarget.dataset)
    const key = e.currentTarget.dataset.key;
    const value = e.currentTarget.dataset.value;
    let selectedProp = this.data.selectedProp
    selectedProp[key] = value
    console.log(selectedProp)
    this.setData({
      selectedProp: selectedProp
    })
    this.chooseArr()
  },
  //切换数组
  chooseArr() {
    let selectedProp = this.data.selectedProp
    let chooseArrs = []
    for (let k in selectedProp) {
      chooseArrs.push(selectedProp[k])
      console.log(selectedProp[k])
    }
    this.setData({
      chooseArrs
    }, () => {
      console.log(this.data.chooseArrs)
    })

  },
  //添加收藏
  onLike() {
    console.log("收藏")
    let prodId = this.data.detailList.prodId
    const token=wx.getStorageSync('token')
    this.setData({
      like: !this.data.like
    })
    Collection.addLike(prodId, (res) => {
      console.log(res)
      // this.setData({
      //   like:!this.data.like
      // })
    },token)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})